Installation & Setup
Install PHP-FPM
bash
1# Ubuntu / Debian (PHP 8.2)2sudo apt update3sudo apt install -y php8.2-fpm php8.2-cli php8.2-common php8.2-mysql php8.2-pgsql \4 php8.2-sqlite3 php8.2-redis php8.2-mbstring php8.2-xml php8.2-curl php8.2-gd \5 php8.2-zip php8.2-intl php8.2-bcmath php8.2-opcache php8.2-readline6
7# Fedora / RHEL8sudo dnf install -y php-fpm php-cli php-common php-mysqlnd php-pgsql \9 php-sqlite3 php-redis php-mbstring php-xml php-curl php-gd \10 php-zip php-intl php-bcmath php-opcache php-readline11
12# macOS (Homebrew)13brew install php14brew services start php15
16# Verify17php -v18php-fpm -vStart & Stop
bash
1# Linux (systemd)2sudo systemctl start php8.2-fpm3sudo systemctl stop php8.2-fpm4sudo systemctl restart php8.2-fpm5sudo systemctl reload php8.2-fpm # Graceful reload (re-read config)6sudo systemctl enable php8.2-fpm7sudo systemctl status php8.2-fpm8
9# Check PHP-FPM status10sudo systemctl status php8.2-fpm11
12# Process management13sudo php-fpm -t # Test configuration syntax14sudo php-fpm -tt # Test with additional detailsImportant Paths & Config Files
bash
1# Ubuntu / Debian2/etc/php/8.2/fpm/php.ini # Main PHP config3/etc/php/8.2/fpm/pool.d/ # Pool configurations4/etc/php/8.2/fpm/pool.d/www.conf # Default pool5/etc/php/8.2/fpm/php-fpm.conf # FPM master config6/var/run/php/php8.2-fpm.sock # Default Unix socket7/var/log/php8.2-fpm.log # FPM log file8/etc/logrotate.d/php8.2-fpm # Log rotation9
10# Fedora / RHEL11/etc/php.ini # Main PHP config12/etc/php.d/ # Additional PHP configs13/etc/php-fpm.d/ # Pool configurations14/etc/php-fpm.d/www.conf # Default pool15/etc/php-fpm.conf # FPM master config16/run/php-fpm/www.sock # Default Unix socket17/var/log/php-fpm/ # Log directory18
19# macOS (Homebrew)20/opt/homebrew/etc/php.ini # Apple Silicon21/opt/homebrew/etc/php-fpm.d/ # Pool configs22/opt/homebrew/etc/php-fpm.conf # FPM config23
24# Show loaded config files25php --ini26php -i | grep "Loaded Configuration File"27php -i | grep "Additional .ini files"Key Configuration (php.ini)
ini
1; /etc/php/8.2/fpm/php.ini2
3; Memory and execution4memory_limit = 256M5max_execution_time = 306max_input_time = 607max_input_vars = 30008
9; File uploads10upload_max_filesize = 64M11post_max_size = 64M12max_file_uploads = 2013
14; Error handling15error_reporting = E_ALL16display_errors = Off ; DANGEROUS in production17display_startup_errors = Off ; DANGEROUS in production18log_errors = On19error_log = /var/log/php8.2-fpm.log20log_errors_max_len = 102421
22; OPcache23opcache.enable=124opcache.memory_consumption=25625opcache.interned_strings_buffer=1626opcache.max_accelerated_files=1000027opcache.revalidate_freq=228opcache.save_comments=129opcache.fast_shutdown=130opcache.enable_cli=031
32; Session33session.save_handler = files34session.save_path = "/var/lib/php/sessions"35session.gc_maxlifetime = 144036session.gc_probability = 137session.gc_divisor = 10038
39; Date40date.timezone = UTC41
42; Security43expose_php = Off44allow_url_fopen = On45allow_url_include = Off46disable_functions = exec,passthru,shell_exec,system,proc_open,popenFPM Pool Configuration
Default Pool (www.conf)
ini
1; /etc/php/8.2/fpm/pool.d/www.conf2
3[www]4; Process user/group5user = www-data6group = www-data7
8; Socket configuration9listen = /var/run/php/php8.2-fpm.sock10listen.owner = www-data11listen.group = www-data12listen.mode = 066013
14; TCP socket alternative15; listen = 127.0.0.1:900016
17; Process manager18pm = dynamic ; static, dynamic, or ondemand19pm.max_children = 50 ; Max simultaneous processes20pm.start_servers = 5 ; Processes at startup21pm.min_spare_servers = 3 ; Min idle processes22pm.max_spare_servers = 10 ; Max idle processes23pm.max_requests = 500 ; Restart after N requests (memory leak prevention)24
25; Status page26pm.status_path = /fpm-status27ping.path = /fpm-ping28
29; Logging30access.log = /var/log/php8.2-fpm-access.log31access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"32
33; Slow log34slowlog = /var/log/php8.2-fpm-slow.log35request_slowlog_timeout = 5s36
37; Security38security.limit_extensions = .php39
40; Environment variables41env[HOSTNAME] = $HOSTNAME42env[PATH] = /usr/local/bin:/usr/bin:/bin43env[TMP] = /tmp44env[TMPDIR] = /tmp45env[TEMP] = /tmpCreate Custom Pool
bash
1# Create new pool: /etc/php/8.2/fpm/pool.d/myapp.conf2cat > /etc/php/8.2/fpm/pool.d/myapp.conf << 'EOF'3[myapp]4user = myapp5group = myapp6
7listen = /var/run/php/myapp.sock8listen.owner = www-data9listen.group = www-data10listen.mode = 066011
12pm = dynamic13pm.max_children = 2014pm.start_servers = 515pm.min_spare_servers = 316pm.max_spare_servers = 1017pm.max_requests = 50018
19php_admin_value[error_log] = /var/log/php8.2-fpm/myapp-error.log20php_admin_flag[log_errors] = on21php_value[session.save_handler] = files22php_value[session.save_path] = "/var/lib/php/sessions/myapp"23php_admin_value[upload_max_filesize] = 128M24php_admin_value[post_max_size] = 128M25php_admin_value[memory_limit] = 512M26EOF27
28# Test and reload29sudo php-fpm -t30sudo systemctl reload php8.2-fpmProcess Management
Status & Monitoring
bash
1# FPM status2sudo php-fpm -t # Test configuration3sudo php-fpm -tt # Test with extra info4
5# Check running processes6ps aux | grep php-fpm7
8# Count PHP-FPM workers9ps aux | grep "php-fpm: pool" | wc -l10
11# Check socket exists12ls -la /var/run/php/php8.2-fpm.sock13
14# Check open files15ls /proc/$(pidof -s php-fpm)/fd | wc -l16
17# Check connections18ss -tlnp | grep 9000 # If using TCP socketReload Without Downtime
bash
1# Graceful reload (re-reads config, spawns new workers, kills old ones)2sudo systemctl reload php8.2-fpm3
4# Equivalent to:5sudo kill -USR2 $(cat /var/run/php/php8.2-fpm.pid)Kill Workers
bash
1# Kill all PHP-FPM workers (DANGEROUS — kills all PHP processes)2sudo systemctl stop php8.2-fpm3
4# Kill specific pool worker5sudo kill <worker-pid>6
7# Restart specific pool (reload config)8sudo systemctl reload php8.2-fpmUseful Patterns
Status Page
ini
1; In pool config (www.conf)2pm.status_path = /fpm-status3ping.path = /fpm-pingnginx
1# Nginx config to expose status page2location /fpm-status {3 allow 127.0.0.1;4 deny all;5 fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;6 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;7 include fastcgi_params;8}9
10location /fpm-ping {11 allow 127.0.0.1;12 deny all;13 fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;14 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;15 include fastcgi_params;16}bash
1# Get FPM status2curl http://127.0.0.1/fpm-status3
4# Get FPM ping5curl http://127.0.0.1/fpm-ping6
7# JSON status8curl -H "Accept: application/json" http://127.0.0.1/fpm-statusSlow Log Analysis
bash
1# Check slow log2tail -50 /var/log/php8.2-fpm-slow.log3
4# Find slow requests (script, time)5grep "script_filename" /var/log/php8.2-fpm-slow.log | sort | uniq -c | sort -rn | head -206
7# Average slow request time8grep "pool www" /var/log/php8.2-fpm-slow.log | awk '{print $NF}' | sort -n | tail -10Health Check
bash
1# Simple health check script2#!/bin/bash3if curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/fpm-ping | grep -q "pong"; then4 echo "PHP-FPM is healthy"5else6 echo "PHP-FPM is DOWN"7 sudo systemctl restart php8.2-fpm8fiOPcache
Configuration
ini
1; /etc/php/8.2/fpm/php.ini2opcache.enable=13opcache.memory_consumption=256 ; 64-512MB4opcache.interned_strings_buffer=16 ; 8-64MB5opcache.max_accelerated_files=10000 ; 2000-1000006opcache.revalidate_freq=2 ; 0 = check every request7opcache.save_comments=1 ; Required for many frameworks8opcache.fast_shutdown=19opcache.enable_cli=0 ; Usually 0 for FPM10opcache.jit=1255 ; PHP 8.0+ JIT11opcache.jit_buffer_size=256MManage OPcache
php
1// Clear OPcache (from PHP script)2opcache_reset(); // Clear entire cache3opcache_invalidate('/path/to/file.php'); // Clear specific file4opcache_get_status(); // Get OPcache stats5
6// Clear OPcache via CLI7php -r "opcache_reset();" // Clear from command linephp
1// OPcache status page (add to a file accessible via web)2<?php3header('Content-Type: application/json');4echo json_encode(opcache_get_status(), JSON_PRETTY_PRINT);Troubleshooting
bash
1# PHP-FPM won't start2sudo php-fpm -t # Check configuration syntax3sudo systemctl status php8.2-fpm # Check service status4sudo journalctl -u php8.2-fpm -n 50 # Check logs5sudo cat /var/log/php8.2-fpm.log | tail -50 # Check FPM log6
7# 502 Bad Gateway (Nginx + PHP-FPM)8# Check socket exists: ls -la /var/run/php/php8.2-fpm.sock9# Check permissions: ls -la /var/run/php/10# Check worker process is running: ps aux | grep php-fpm11# Restart: sudo systemctl restart php8.2-fpm12
13# Too many open files (EMFILE)14# Increase worker_rlimit_nofile in php-fpm.conf15# Increase systemd LimitNOFILE16sudo systemctl edit php8.2-fpm17# [Service]18# LimitNOFILE=6553519
20# PHP process hanging21# Increase request_slowlog_timeout22# Check slow log for long-running scripts23tail -f /var/log/php8.2-fpm-slow.log24
25# Memory exhaustion (Allowed memory size exhausted)26php_admin_value[memory_limit] = 512M ; Increase in pool config27php -i | grep memory_limit ; Check current limit28
29# File permission issues30ls -la /var/run/php/ # Check socket ownership31ls -la /var/log/php8.2-fpm.log # Check log permissions32chown www-data:www-data /var/run/php/ # Fix socket permissionsIntegration with Nginx
FastCGI Pass (Unix Socket)
nginx
1server {2 listen 80;3 server_name example.com;4 root /var/www/example.com/public;5 index index.php index.html;6
7 location / {8 try_files $uri $uri/ /index.php?$query_string;9 }10
11 location ~ \.php$ {12 fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;13 fastcgi_index index.php;14 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;15 include fastcgi_params;16
17 # Timeouts18 fastcgi_connect_timeout 60s;19 fastcgi_send_timeout 60s;20 fastcgi_read_timeout 60s;21
22 # Buffers23 fastcgi_buffer_size 128k;24 fastcgi_buffers 4 256k;25 fastcgi_busy_buffers_size 256k;26 }27
28 # Deny access to .htaccess29 location ~ /\.ht {30 deny all;31 }32}FastCGI Pass (TCP Socket)
nginx
1server {2 listen 80;3 server_name example.com;4 root /var/www/example.com/public;5
6 location / {7 try_files $uri $uri/ /index.php?$query_string;8 }9
10 location ~ \.php$ {11 fastcgi_pass 127.0.0.1:9000;12 fastcgi_index index.php;13 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;14 include fastcgi_params;15 }16}Integration with Apache
Apache + PHP-FPM (mod_proxy_fcgi)
apache
1# /etc/apache2/sites-available/example.com.conf2<VirtualHost *:80>3 ServerName example.com4 DocumentRoot /var/www/example.com/public5
6 <Directory /var/www/example.com/public>7 AllowOverride All8 Require all granted9 </Directory>10
11 # Proxy PHP to PHP-FPM12 <FilesMatch \.php$>13 SetHandler "proxy:unix:/var/run/php/php8.2-fpm.sock|fcgi://localhost"14 </FilesMatch>15
16 # Or use TCP socket17 # <FilesMatch \.php$>18 # SetHandler "proxy:fcgi://127.0.0.1:9000"19 # </FilesMatch>20</VirtualHost>bash
1# Enable modules2sudo a2enmod proxy_fcgi setenvif3sudo a2enconf php8.2-fpm4sudo systemctl restart apache2Performance Tuning
Process Manager Modes
| Mode | Description | Best For |
|---|---|---|
static | Fixed number of workers | Predictable load |
dynamic | Workers adjust based on load | Most workloads |
ondemand | Workers spawn on demand, idle killed | Low-traffic servers |
ini
1; Static mode (consistent, higher memory)2pm = static3pm.max_children = 504
5; Dynamic mode (adaptive)6pm = dynamic7pm.max_children = 508pm.start_servers = 109pm.min_spare_servers = 510pm.max_spare_servers = 1511pm.max_requests = 50012
13; Ondemand mode (lowest memory, highest latency)14pm = ondemand15pm.max_children = 5016pm.process_idle_timeout = 10s17pm.max_requests = 500Worker Calculation
bash
1# General rule: max_children = (Available RAM - OS/Other processes) / PHP per worker2# Check PHP per-worker memory:3php -i | grep memory_limit # Per-script limit4# Typical PHP-FPM worker uses 20-50MB RSS5
6# Example: 4GB server, 1GB for OS/other7# Available: 3GB = 3072MB8# Per worker: ~30MB9# max_children = 3072 / 30 ≈ 10010# But set lower: 50-70 for headroomMemory Monitoring
bash
1# Check per-process memory2ps aux | grep "php-fpm: pool" | awk '{sum+=$6; count++} END {print "Total:", sum/1024, "MB, Per-worker:", (sum/count)/1024, "MB"}'3
4# Monitor PHP memory usage5php -r "echo memory_get_peak_usage() . ' bytes' . PHP_EOL;"Useful Commands
bash
1# PHP version and modules2php -v3php -m # List loaded modules4php -i | grep -i opcache # Check OPcache config5php --ini # Show loaded ini files6
7# PHP-FPM specific8php-fpm -t # Test config9php-fpm -tt # Test with extra info10php-fpm -h # Help11
12# Socket status13ss -tlnp | grep 9000 # TCP socket14ls -la /var/run/php/ # Unix socket15
16# Log monitoring17tail -f /var/log/php8.2-fpm.log # FPM log18tail -f /var/log/php8.2-fpm-access.log # Access log19tail -f /var/log/php8.2-fpm-slow.log # Slow log20
21# Check active connections22curl -s http://127.0.0.1/fpm-status | grep "active processes"